<?php
class Template{
    
    private $_data = array();
    /**
     * @var Zend_Cache_Core
     */
    static protected $_cache = false;
    protected $_useCache = true;
    
    static public function setCache(Zend_Cache_Core $manager){
        self::$_cache = $manager;
    }
    
    public function render($path){
        $hash = '';
        if(self::$_cache && $this->_useCache){
            $hash = md5('tpl_'.$path.'_'.serialize($this->_data));
            $html = self::$_cache->load($hash);
            if($html !==false)
                return $html;
        }
        
        ob_start();
        include $path;
        $result = ob_get_clean();
        
        if(self::$_cache && $this->_useCache)
            self::$_cache->save($result , $hash);
        
        
        return $result;
    }
    
    public function __set($name , $value){
        $this->_data[$name] = $value;
    }
    
    public function __isset($name){
        return isset($this->_data[$name]);
    }
    
    public function __get($name){
       return  $this->_data[$name];  
    }
    
    public function __unset($name){
        unset($this->_data[$name]);
    }
    
    public function clear(){
        $this->_data = array();
    }
    
    public function disableCache(){
        $this->_useCache = false;
    }
    public function enableCache(){
        $this->_useCache = true;
    }
}